﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

public partial class Account_Register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }


    private bool ValidateInputData()
    {
        bool regFlag = true;

        //验证账号
        if (Regex.IsMatch(txt_userName.Value.Trim(), "^[a-zA-Z0-9]{4,15}$"))
            regFlag = regFlag & true;
        else
        {
            Common.AlertMessage("用户名英文或数字,长度为4~14的字符！");
            return false;
        }

        //验证密码
        if (Regex.IsMatch(txt_Password.Value.Trim(), "^[a-zA-Z0-9]{6,16}$"))
            regFlag = regFlag & true;
        else
        {
            Common.AlertMessage("密码不符合规范，密码必须为6~16位英文+数字的组合！");
            return false;
        }

        if (txt_Password.Value.Trim() == txt_RePassword.Value.Trim())
            regFlag = regFlag & true;
        else
        {
            Common.AlertMessage("确认密码与密码不一致，请重新填写");
            return false;
        }

        ////验证姓名
        //if (txtRealName.Value.Trim().Length > 0)
        //{
        //    if (Regex.IsMatch(txtRealName.Value.Trim(), @"^[\u4e00-\u9fa5]{2,10}$", RegexOptions.IgnoreCase))
        //        regFlag = regFlag & true;
        //    else
        //    {
        //        Common.AlertMessage("真实姓名必须是中文");
        //        return false;
        //    }
        //}

        ////验证电话
        //if (txtPhone.Value.Trim().Length > 0)
        //{
        //    if (Regex.IsMatch(txtPhone.Value.Trim(), @"[0-9]{6,12}$"))
        //        regFlag = regFlag & true;
        //    else
        //    {
        //        Common.AlertMessage("电话输入不合法");
        //        return false;
        //    }
        //}

        ////验证msn
        //if (txtMsn.Value.Trim().Length > 0)
        //{
        //    if (Regex.IsMatch(txtMsn.Value.Trim(), @"^.{1,}[@].{1,}[.][a-zA-Z]{1,}"))
        //        regFlag = regFlag & true;
        //    else
        //    {
        //        Common.AlertMessage("MSN输入错误");
        //        return false;
        //    }
        //}

        return regFlag;
    }
    protected void btn_Register_Click(object sender, EventArgs e)
    {
        try
        {

            if (string.Equals(txtValidate.Value.Trim().ToLower(), Session["CAPTCHATEXT"] == null ? txtValidate.Value.Trim().ToLower() : Session["CAPTCHATEXT"].ToString().ToLower()) == false)
            {
                Common.AlertMessage("验证码错误！");
                return;
            }
            if (ValidateInputData())
            {
                int ret = DataHandler.Current.RegUser(txt_userName.Value.Trim(), txt_Password.Value.Trim(), txt_birthday.Value.Trim(), Convert.ToInt16(sel_sex.Value), txt_ssn.Value.Trim());
                if (ret == 1)
                {
                    Common.AlertMessageAndRedirect("注册成功！", ResolveUrl("../Account/Login.aspx"));
                }
                else
                { Common.AlertMessage("系统错误请稍候再试！");
                }
            }
        }
        catch(Exception ex)
        {
            //Logger.WriteLog(string.Format("{0}_(1)_{2}", ClassFactory.ICommonEngine.GetMethodLocateFileName(),
            //   ClassFactory.ICommonEngine.GetMethodName(), ex.Message));
            Common.AlertMessage("系统错误请稍候再试！"+ex.Message);
            return;
        }
    }
}